﻿using System;
using System.Linq;
using System.Web.Mvc;
using Project.DAL.Repository;
using Project.Domain.Entities;

namespace Project.WebUI.Areas.Admin.Controllers
{
    public class SecureStoreController : Controller
    {        
        #region "private"

        private readonly IStoreSecureRepositoty _storeSecureRepositoty;

        public SecureStoreController(IStoreSecureRepositoty storeSecureRepositoty)
        {
            _storeSecureRepositoty = storeSecureRepositoty;
        }

        #endregion    

        #region LoadAjax
        public ActionResult LoadAjax(int page)
        {
            var store = _storeSecureRepositoty.GetAll().FirstOrDefault();
            ViewBag.result = _storeSecureRepositoty.GetAll().Count();
            return PartialView("_MainContent", store);
        }
        #endregion

        #region Modify Obj
        public ActionResult Modify(int? id)
        {
            if (Session["admin"] == null)
                return RedirectToAction("Login", "DashBoard");
            if (id != null)
            {
                var obj = _storeSecureRepositoty.GetById((int) id);
                return View(obj);
            }
            return View();
        }

        [HttpPost]
        public ActionResult Modify(int? id, FormCollection obj)
        {
            var storePayto = obj["StorePayto"];
            var machentStoreName = obj["MachentStoreName"];
            var secureWord = obj["SecureWord"];
            if (id != null)
            {
                var store = _storeSecureRepositoty.GetAll().FirstOrDefault();
                store.ConfigStorePayto = storePayto;
                store.ConfigMachentStoreName = machentStoreName;
                store.ConfigSecureWord = secureWord;
                _storeSecureRepositoty.SaveChanges();
                //sucessfull
                return Content("1");
            }
            else
            {
                try
                {
                    var store = new StoreSecure()
                        {
                            ConfigStorePayto = storePayto,
                            ConfigMachentStoreName = machentStoreName,
                            ConfigSecureWord = secureWord
                        };
                    _storeSecureRepositoty.Add(store);
                    _storeSecureRepositoty.SaveChanges();
                    return Content("1");
                }
                catch (Exception)
                {
                    return Content("0");
                }
            }
        }

        #endregion
        public ActionResult Index()
        {
            return View();
        }

    }
}
